import request from '@/utils/request'

// 查询故障报修列表
export function listFaultReport(query) {
  return request({
    url: '/system/fault-report/list',
    method: 'get',
    params: query
  })
}

// 查询故障报修详细
export function getFaultReport(reportNo) {
  return request({
    url: '/system/fault-report/' + reportNo,
    method: 'get'
  })
}

// 新增故障报修
export function addFaultReport(data) {
  return request({
    url: '/system/fault-report',
    method: 'post',
    data: data
  })
}

// 修改故障报修
export function updateFaultReport(data) {
  return request({
    url: '/system/fault-report',
    method: 'put',
    data: data
  })
}

// 删除故障报修
export function delFaultReport(reportNo) {
  return request({
    url: '/system/fault-report/' + reportNo,
    method: 'delete'
  })
}

// 提交故障报修
export function submitFaultReport(reportNo) {
  return request({
    url: '/system/fault-report/submit/' + reportNo,
    method: 'post'
  })
}

// 撤回故障报修
export function withdrawFaultReport(reportNo) {
  return request({
    url: '/system/fault-report/withdraw/' + reportNo,
    method: 'post'
  })
}

// 承认故障报修
export function acknowledgeFaultReport(reportNo) {
  return request({
    url: '/system/fault-report/acknowledge/' + reportNo,
    method: 'post'
  })
}

// 拒绝承认故障报修
export function rejectFaultReport(reportNo) {
  return request({
    url: '/system/fault-report/reject/' + reportNo,
    method: 'post'
  })
}

// 获取故障报修状态履历
export function getFaultReportHistory(reportNo) {
  return request({
    url: '/system/fault-report/' + reportNo + '/history',
    method: 'get'
  })
}

// 检查设备是否有未完成的报修
export function checkUnfinishedReport(assetNo) {
  return request({
    url: '/system/fault-report/check-unfinished/' + assetNo,
    method: 'get'
  })
}

// 上传故障报修图片，返回文件名
export function uploadFaultImage(filePath) {
  return new Promise((resolve, reject) => {
    try {
      const baseURL = (uni.getStorageSync('BASE_URL') || 'http://127.0.0.1:8081').replace(/\/$/, '')
      const token = uni.getStorageSync('token') || uni.getStorageSync('Authorization') || uni.getStorageSync('Admin-Token') || ''
      
      // 使用MinIO上传接口
      const uploadUrl = baseURL + '/minio/upload'
      console.log('故障报修使用MinIO上传接口:', uploadUrl)
      
      uni.uploadFile({
        url: uploadUrl,
        filePath,
        name: 'file',
        fileType: 'image',
        header: {
          Authorization: token ? `Bearer ${token}` : undefined,
          Token: token
        },
        success: (res) => {
          console.log('故障报修MinIO上传响应:', res)
          try {
            const data = JSON.parse(res.data || '{}')
            const ok = ((res.statusCode === 200 || res.statusCode === 201)) && (data.code === undefined || data.code === 200)
            const fileName = data?.data?.fileName || data?.data?.name || data?.fileName || data?.name || data?.data
            if (ok && fileName) {
              console.log('故障报修MinIO上传成功，文件名:', fileName)
              return resolve(String(fileName))
            }
            reject(data)
          } catch (e) {
            if (res.statusCode === 200 && typeof res.data === 'string') {
              console.log('故障报修MinIO上传成功，直接返回:', res.data)
              return resolve(res.data)
            }
            reject(res)
          }
        },
        fail: (error) => {
          console.log('故障报修MinIO上传失败:', error)
          reject(error)
        }
      })
    } catch (e) { reject(e) }
  })
}

